package com.engine.jz.controller;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.jz.service.JzjgService;
import com.engine.jz.service.impl.JzjgServiceImpl;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/11/28
 * @Description
 * @Author AdminZm
 */
public class JzjgController {

    public JzjgService getJzjgService(User user) {
        return ServiceUtil.getService(JzjgServiceImpl.class, user);
    }

    /**
     * 获取旷视门禁数据
     */
    @POST
    @Path("/getKsDoorData")
    @Produces(MediaType.APPLICATION_JSON)
    public String getKsDoorData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> params = ParamUtil.request2Map(request);
        Map<String, Object> resultMap = getJzjgService(user).getKsDoorData(params);
        return  new Gson().toJson(resultMap);
    }

    /**
     * 获取天波门禁数据
     */
    @POST
    @Path("/getTbDoorData")
    @Produces(MediaType.APPLICATION_JSON)
    public String getTbDoorData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> params = ParamUtil.request2Map(request);
        Map<String, Object> resultMap = getJzjgService(user).getTbDoorData(params);
        return  new Gson().toJson(resultMap);
    }

    /**
     * 重新推送
     */
    @POST
    @Path("/pushAgain")
    @Produces(MediaType.APPLICATION_JSON)
    public String pushAgain(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> params = ParamUtil.request2Map(request);
        Map<String, Object> resultMap = getJzjgService(user).pushAgain(params);
        return  new Gson().toJson(resultMap);
    }

    /**
     * 推送人员去指定设备
     */
    @POST
    @Path("/pushHrmToAppointDevice")
    @Produces(MediaType.APPLICATION_JSON)
    public String pushHrmToAppointDevice(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> params = ParamUtil.request2Map(request);
        Map<String, Object> resultMap = getJzjgService(user).pushHrmToAppointDevice(params);
        return  new Gson().toJson(resultMap);
    }

    /**
     * 删除指定设备中的人员
     */
    @POST
    @Path("/delHrmInAppointDevice")
    @Produces(MediaType.APPLICATION_JSON)
    public String delHrmInAppointDevice(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> params = ParamUtil.request2Map(request);
        Map<String, Object> resultMap = getJzjgService(user).delHrmInAppointDevice(params);
        return  new Gson().toJson(resultMap);
    }

    /**
     * 删除指定设备中的人员(新)
     */
    @POST
    @Path("/delHrmInAppointDeviceNew")
    @Produces(MediaType.APPLICATION_JSON)
    public String delHrmInAppointDeviceNew(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> params = ParamUtil.request2Map(request);
        Map<String, Object> resultMap = getJzjgService(user).delHrmInAppointDeviceNew(params);
        return  new Gson().toJson(resultMap);
    }

    @POST
    @Path("/putrytx")
    @Produces(MediaType.APPLICATION_JSON)
    public String putRytx(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog("putRytx start.");
        Map<String, Object> resultDatas = new HashMap<>();
        try {
            Map<String, Object> params = ParamUtil.request2Map(request);
            baseBean.writeLog("putRytx params:" + params);
            String ry = Util.null2String(params.get("sfzhm"));
            String qqId = Util.null2String(params.get("qqid"));
            if (StringUtils.isNotEmpty(qqId)) {
                RecordSet rs = new RecordSet();
                RecordSet rs1 = new RecordSet();
                String tableName = null;
                rs.execute("SELECT \n" +
                        "    b.tablename \n" +
                        "FROM \n" +
                        "    workflow_requestbase a\n" +
                        "    JOIN workflow_base c ON a.workflowid = c.id\n" +
                        "    JOIN workflow_bill b ON c.formid = b.id\n" +
                        "WHERE \n" +
                        "    a.requestid = " + qqId);
                if (rs.next()) {
                    tableName = rs.getString("tablename");
                }
                if (StringUtils.isEmpty(tableName)) {
                    return new Gson().toJson(resultDatas);
                }
                if (StringUtils.isNotEmpty(ry)) {
                    String zpId = null;
                    String dataId = null;
                    rs.execute("SELECT id, rytx FROM uf_rzqxxcjgljmb WHERE sfzhm = '" + ry + "'");
                    if (rs.next()) {
                        zpId = rs.getString("rytx");
                        dataId = rs.getString("id");
                    }
                    if (StringUtils.isNotEmpty(zpId)) {
                        rs.execute("UPDATE " + tableName + " SET rytx = '" + zpId + "' WHERE requestid = " + qqId);
                    } else {
                        rs.execute("UPDATE " + tableName + " SET rytx = null WHERE requestid = " + qqId);
                    }

                    if (StringUtils.isNotEmpty(dataId)) {
                        Map<String, String> fjDt1Map = new HashMap<>();
                        rs.execute("SELECT id, fjzl FROM uf_rzqxxcjgljmb_dt1 WHERE mainid = " + dataId);
                        while (rs.next()) {
                            String dt1Id = rs.getString("id");
                            String fjzl = rs.getString("fjzl");
                            if (StringUtils.isNotEmpty(dt1Id) && StringUtils.isNotEmpty(fjzl)) {
                                fjDt1Map.put(dt1Id, fjzl);
                            }
                        }
                        if (!CollectionUtils.isEmpty(fjDt1Map)) {
                            rs.execute("SELECT a.id, a.jmmxid FROM " + tableName + "_dt1 a, " + tableName + " b " +
                                    "WHERE a.mainid = b.id AND b.requestid = " + qqId);
                            while (rs.next()) {
                                String dt1Id = rs.getString("id");
                                String jmmxid = rs.getString("jmmxid");
                                if (StringUtils.isEmpty(dt1Id) || StringUtils.isEmpty(jmmxid)) {
                                    continue;
                                }
                                String fjzl = fjDt1Map.get(jmmxid);
                                if (StringUtils.isNotEmpty(fjzl)) {
                                    rs1.executeUpdate("UPDATE " + tableName + "_dt1 SET fjzl = ? WHERE id = ?", fjzl, dt1Id);
                                }
                            }

                        }
                        Map<String, String> fjDt2Map = new HashMap<>();
                        rs.execute("SELECT id, fj FROM uf_rzqxxcjgljmb_dt2 WHERE mainid = " + dataId);
                        while (rs.next()) {
                            String dt2Id = rs.getString("id");
                            String fjzl = rs.getString("fj");
                            if (StringUtils.isNotEmpty(dt2Id) && StringUtils.isNotEmpty(fjzl)) {
                                fjDt2Map.put(dt2Id, fjzl);
                            }
                        }
                        if (!CollectionUtils.isEmpty(fjDt2Map)) {
                            rs.execute("SELECT a.id, a.jmmxid FROM " + tableName + "_dt2 a, " + tableName + " b " +
                                    "WHERE a.mainid = b.id AND b.requestid = " + qqId);
                            while (rs.next()) {
                                String dt2Id = rs.getString("id");
                                String jmmxid = rs.getString("jmmxid");
                                if (StringUtils.isEmpty(dt2Id) || StringUtils.isEmpty(jmmxid)) {
                                    continue;
                                }
                                String fjzl = fjDt2Map.get(jmmxid);
                                if (StringUtils.isNotEmpty(fjzl)) {
                                    rs1.executeUpdate("UPDATE " + tableName + "_dt2 SET fj = ? WHERE id = ?", fjzl, dt2Id);
                                }
                            }

                        }
                        Map<String, String> fjDt3Map = new HashMap<>();
                        rs.execute("SELECT id, xgzl FROM uf_rzqxxcjgljmb_dt3 WHERE mainid = " + dataId);
                        while (rs.next()) {
                            String dt3Id = rs.getString("id");
                            String fjzl = rs.getString("xgzl");
                            if (StringUtils.isNotEmpty(dt3Id) && StringUtils.isNotEmpty(fjzl)) {
                                fjDt3Map.put(dt3Id, fjzl);
                            }
                        }
                        if (!CollectionUtils.isEmpty(fjDt3Map)) {
                            rs.execute("SELECT a.id, a.jmmxid FROM " + tableName + "_dt3 a, " + tableName + " b " +
                                    "WHERE a.mainid = b.id AND b.requestid = " + qqId);
                            while (rs.next()) {
                                String dt3Id = rs.getString("id");
                                String jmmxid = rs.getString("jmmxid");
                                if (StringUtils.isEmpty(dt3Id) || StringUtils.isEmpty(jmmxid)) {
                                    continue;
                                }
                                String fjzl = fjDt3Map.get(jmmxid);
                                if (StringUtils.isNotEmpty(fjzl)) {
                                    rs1.executeUpdate("UPDATE " + tableName + "_dt3 SET xgzl = ? WHERE id = ?", fjzl, dt3Id);
                                }
                            }

                        }
                    }
                } else {
                    rs.execute("UPDATE " + tableName + " SET rytx = null WHERE requestid = " + qqId);
                }
            }
        } catch (Exception e) {
            baseBean.writeLog("putRytx error:" + e);
        }
        return  new Gson().toJson(resultDatas);
    }

    /**
     * 获取设备上的所有人员
     */
    @POST
    @Path("/getDeviceAllHrm")
    @Produces(MediaType.APPLICATION_JSON)
    public String getDeviceAllHrm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> params = ParamUtil.request2Map(request);
        Map<String, Object> resultMap = getJzjgService(user).getDeviceAllHrm(params);
        return  new Gson().toJson(resultMap);
    }

    /**
     * 食堂重新推送
     */
    @POST
    @Path("/canteenPushAgain")
    @Produces(MediaType.APPLICATION_JSON)
    public String canteenPushAgain(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> params = ParamUtil.request2Map(request);
        Map<String, Object> resultMap = getJzjgService(user).canteenPushAgain(params);
        return  new Gson().toJson(resultMap);
    }
}
